#ifndef SIGNTREE_TABLE_MODEL_H
#define SIGNTREE_TABLE_MODEL_H

#include <QAbstractItemModel>
#include <QSize>
#include <vector>
#include "model/sdf/SDFSign.h"
#include "StringCovert.h"


using namespace std;

namespace xpm {
	const int Column_Size = 4;
	class SignTreeModel : public QAbstractItemModel{
	public:
		SignTreeModel(QObject* object, SDFSign* root = 0) : 
		  QAbstractItemModel(object), _root(root){}
		  SDFSign* root();
		  void setRoot(SDFSign* root);
		  int rowCount(const QModelIndex& parent) const;
		  int columnCount(const QModelIndex& parent) const;
		  QVariant data(const QModelIndex& index, int role) const;
		  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
		  Qt::ItemFlags flags(const QModelIndex &index) const;
		  QModelIndex index(int row, int column,
			  const QModelIndex &parent = QModelIndex()) const;
		  QModelIndex parent(const QModelIndex &index) const;
	private:	
		QVariant type2str(int type) const;
		QVariant type2value(int type, SDFSign *item) const;
		void GetPage();	
		SDFSign* _root;
		vector<vector<QString> > _rows;
	};

	inline SDFSign* SignTreeModel::root() {
		return _root;
	}

	inline void SignTreeModel::setRoot(SDFSign* root) {
		_root = root;
	}
	inline int SignTreeModel::rowCount(const QModelIndex& parent) const {
		SDFSign *parentItem;
		
		if (!parent.isValid())
			parentItem = _root;
		else
			parentItem = static_cast<SDFSign*>(parent.internalPointer());

		int rowCount = parentItem->size();
		return rowCount;
	}

	inline int SignTreeModel::columnCount(const QModelIndex& parent) const {
		return Column_Size;
	}


	inline QVariant SignTreeModel::headerData(int section, Qt::Orientation orientation, int role) const {
		const QString headers[] = {"Denotation", "Type", "Type Name", "Reification"};
		if(role == Qt::DisplayRole) { 
			if(orientation == Qt::Horizontal)		
				return headers[section];
		}
		return QVariant::Invalid;
	}

	inline Qt::ItemFlags SignTreeModel::flags(const QModelIndex &index) const
	{
		if (!index.isValid())
			return 0;

		return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
	}
}
#endif
